package com.ad.pet.common.data.utils.glide;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.DrawableTypeRequest;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.target.ViewTarget;
import com.bumptech.glide.signature.StringSignature;
import com.ad.pet.Logger;
import com.ad.pet.R;
import com.ad.pet.common.data.http.glide.BlurTransformation;
import com.ad.pet.common.data.http.glide.GlideCircleTransform;
import com.ad.pet.common.data.http.glide.GlideRoundTransform;
import com.ad.pet.common.data.utils.DensityUtil;
import com.ad.pet.common.data.utils.SnackBarUtil;

import java.io.File;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;


/**
 * Created by ems on 2017/3/1.
 */

public class GlideUtil {
    private static final String TAG = "GlideUtil";
    private static int ERROR_RESOURCES_ID = R.color.bg_color;
    private static int DEFAULT_RESOURCES_ID = R.color.bg_color;
    private static int ERROR_GROUP_RESOURCES_ID = -1;


    public static void load(Fragment fragment, String path, ImageView imageView) {
        Glide.with(fragment).load(path).fitCenter().placeholder(DEFAULT_RESOURCES_ID)
                .error(ERROR_RESOURCES_ID).into(imageView);
    }

    public static void load(Context context, String path, ImageView imageView) {
        Glide.with(context).load(path)
                .centerCrop().placeholder(DEFAULT_RESOURCES_ID)
                .error(ERROR_RESOURCES_ID).into(imageView);
    }

    public static void loadRGB(Context context, String path, ImageView imageView) {
        Glide.with(context).load(path).asBitmap().format(DecodeFormat.PREFER_ARGB_8888)
                .centerCrop().placeholder(DEFAULT_RESOURCES_ID).diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(ERROR_RESOURCES_ID).into(imageView);
    }
    public static void loadRGBNoAnime(Context context, String path, ImageView imageView) {
        Glide.with(context).load(path).asBitmap().format(DecodeFormat.PREFER_ARGB_8888)
                .placeholder(DEFAULT_RESOURCES_ID).diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(ERROR_RESOURCES_ID).into(imageView);
    }
    public static void loadSkipMemery(Context context, String path, ImageView imageView) {

        Glide.with(context).load(path)
                .centerCrop().placeholder(DEFAULT_RESOURCES_ID)
                .error(ERROR_RESOURCES_ID).skipMemoryCache(true).diskCacheStrategy( DiskCacheStrategy.NONE ).signature(new StringSignature(System.currentTimeMillis()+"")).into(imageView);
    }
    public static void loadNoDefault(Context context, String path, ImageView imageView) {
        Glide.with(context).load(path)
                .fitCenter()
                .error(ERROR_RESOURCES_ID).into(imageView);
    }

    public static void load(Context context, Uri path, ImageView imageView) {
        Glide.with(context).load(path)
                .fitCenter().placeholder(DEFAULT_RESOURCES_ID)
                .error(ERROR_RESOURCES_ID).into(imageView);
    }

    public static void load(Context context, String path, ImageView imageView
            , int defaultId, int errorId) {
        Glide.with(context).load(path)
                .fitCenter().placeholder(defaultId)
                .error(errorId).into(imageView);
    }

    public static void load(Context context, String path, ImageView imageView
            , int defaultId) {
        Glide.with(context).load(path)
                .fitCenter().placeholder(defaultId)
                .error(ERROR_RESOURCES_ID).into(imageView);
    }
    public static void load(Context context, byte[] path, ImageView imageView) {
        Glide.with(context).load(path)
                .fitCenter().placeholder(DEFAULT_RESOURCES_ID)
                .error(ERROR_RESOURCES_ID).into(imageView);
    }
    //同步方法
//    public static GlideDrawable loadOther(Context context, String path) throws ExecutionException, InterruptedException {
//        return Glide.with(context).load(path)
//                .placeholder(DEFAULT_RESOURCES_ID)
//                .fitCenter().error(ERROR_RESOURCES_ID).into(400, 400).get();
//    }

//    public static void loadDrawable(Context context, String path, final View view) {
//        Glide.with(context).load(path)
//                .placeholder(DEFAULT_RESOURCES_ID)
//                .error(ERROR_RESOURCES_ID)
//                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .listener(new com.bumptech.glide.request.RequestListener<String, GlideDrawable>() {
//
//                    @Override
//                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
//                        view.setBackgroundResource(ERROR_RESOURCES_ID);
//                        return false;
//                    }
//
//                    //加载完成
//                    @Override
//                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
//                        view.setBackground(resource);
//                        return false;
//                    }
//                })
//                .fitCenter().into(400, 400);
//    }

    public static void loadDrawable(Context context, File path, final View view) {
        Glide.with(context).load(path)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(DEFAULT_RESOURCES_ID)
                .error(ERROR_RESOURCES_ID).override(400, 400).into(new ViewTarget<View, GlideDrawable>(view) {
            @Override
            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                view.setBackground(resource);
            }
        });
    }

    public static void loadBlurDrawable(final Activity activity, String url, final View layout) {
        Glide.with(activity).load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(DEFAULT_RESOURCES_ID)
                .bitmapTransform(new BlurTransformation(activity, 29, 4))
                .error(ERROR_RESOURCES_ID)
                .into(new ViewTarget<View, GlideDrawable>(layout) {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        if (activity == null || activity.isFinishing() || layout == null) {
                            return;
                        }
                        layout.setBackground(resource);
                    }
                });
    }

    public static void loadDrawable(final Context context, String path, final ImageView imageView) {
        Glide.with(context).load(path)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(DEFAULT_RESOURCES_ID)
                .error(ERROR_RESOURCES_ID).override(400, 400).into(new ViewTarget<View, GlideDrawable>(imageView) {
            @Override
            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {

                Point screenSize = DensityUtil.getScreenSize();
                int screenWidth = screenSize.x / 3;
                int screenHeight = screenSize.y / 3;

                int intrinsicHeight = resource.getIntrinsicHeight();
                int intrinsicWidth = resource.getIntrinsicWidth();

                imageView.setImageDrawable(resource);
            }
        });
    }

    //同步方法
    public static Bitmap loadBitmap(Context context, String path) throws ExecutionException, InterruptedException {
        return loadBitmap(context, path, 200, 200);
    }

    //同步方法
    public static Bitmap loadBitmap(Context context, String path, int w, int h) throws ExecutionException, InterruptedException {
        return Glide.with(context).load(path).asBitmap()
                .fitCenter().placeholder(DEFAULT_RESOURCES_ID)
                .error(ERROR_RESOURCES_ID).into(w, h).get();
    }

    public static void load(Fragment fragment, File file, ImageView imageView) {
        Glide.with(fragment).load(file).fitCenter().placeholder(DEFAULT_RESOURCES_ID)
                .error(ERROR_RESOURCES_ID).into(imageView);
    }

    public static void load(Context activity, int  path, ImageView imageView) {
        Glide.with(activity).load(path).transform(new GlideRoundTransform(activity)).into(imageView);
    }
    public static void load(Context context, File file, ImageView imageView) {
        Glide.with(context).load(file).fitCenter().placeholder(DEFAULT_RESOURCES_ID)
                .error(ERROR_RESOURCES_ID).into(imageView);
    }

    public static void loadCircle(Fragment fragment, String path, ImageView iv) {
        Glide.with(fragment).load(path).error(ERROR_RESOURCES_ID).placeholder(DEFAULT_RESOURCES_ID)
                .fitCenter().transform(new GlideCircleTransform(fragment.getActivity()))
                .crossFade().into(iv);
    }

    public static void loadCircle(Fragment fragment, File path, ImageView iv) {
        Glide.with(fragment).load(path).error(ERROR_RESOURCES_ID).placeholder(DEFAULT_RESOURCES_ID)
                .fitCenter().transform(new GlideCircleTransform(fragment.getActivity())).into(iv);
    }

    public static void loadCircle(Context context, File path, ImageView iv) {
        Glide.with(context).load(path).error(ERROR_RESOURCES_ID).placeholder(DEFAULT_RESOURCES_ID)
                .fitCenter().transform(new GlideCircleTransform(context)).into(iv);
    }

    public static void loadCircle(Context context, String path, ImageView iv) {
        Glide.with(context).load(path).error(ERROR_RESOURCES_ID).placeholder(DEFAULT_RESOURCES_ID)
                .fitCenter().transform(new GlideCircleTransform(context)).into(iv);
    }

    public static void loadCircleNoPlaceholder(Context context, String path, ImageView iv) {
        Glide.with(context).load(path).error(ERROR_RESOURCES_ID)
                .fitCenter().transform(new GlideCircleTransform(context)).into(iv);
    }
    public static void loadRound(Context context, String path, ImageView iv) {
        Glide.with(context).load(path).error(ERROR_RESOURCES_ID).placeholder(DEFAULT_RESOURCES_ID)
                .transform(new CenterCrop(context),new GlideRoundTransform(context)).into(iv);
    }
    public static void loadRoundNoPlaceholder(Context context, String path, ImageView iv) {
        Glide.with(context).load(path).error(ERROR_RESOURCES_ID)
                .fitCenter().transform(new GlideRoundTransform(context)).into(iv);
    }

    public static void loadThumbnail(Context context, final String compressUrl, final String originUrl, final ImageView imageview
            , final View progress) {
        DrawableTypeRequest<String> compressRequest = Glide.with(context).load(compressUrl);
        Glide.with(context)
                .load(originUrl)
                .thumbnail(compressRequest)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        if (progress != null) progress.setVisibility(View.GONE);
                        Logger.e("onException compressUrl:" + compressUrl + " originUrl:" + originUrl + "  exception: " + Log.getStackTraceString(e));
                        SnackBarUtil.showStatic(imageview, "加载图片失败");
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {

                        if (progress != null) progress.setVisibility(View.GONE);
                        Logger.d("onResourceReady");
                        return false;
                    }
                }).into(imageview);

    }
    public static void loadDefault(Context context, String path, ImageView imageView) {
        Glide.with(context).load(path)
                .placeholder(DEFAULT_RESOURCES_ID)
                .error(ERROR_RESOURCES_ID).dontAnimate().into(imageView);
    }
    public static void loadDefaultNoAnime(Context context, String path, ImageView imageView) {
        Glide.with(context).load(path)
                .placeholder(DEFAULT_RESOURCES_ID)
                .error(ERROR_RESOURCES_ID).dontAnimate().into(imageView);
    }

    public static void loadThumbnail(Context context, String compressUrl, String originUrl, ImageView imageview) {
        loadThumbnail(context, compressUrl, originUrl, imageview, null);

    }

    public static void loadBlur(Context context, String url, ImageView imageview) {
        BlurTransformation transformation = new BlurTransformation(context);
        Glide.with(context)
                .load(url)
                .bitmapTransform(transformation)
                .into(imageview);
    }

    public static void loadThumbnailAddListener(Context context, String compressUrl, String originUrl, final ImageView imageview
            , RequestListener<String, GlideDrawable> listener) {
        DrawableTypeRequest<String> compressRequest = Glide.with(context).load(compressUrl);
        Glide.with(context)
                .load(originUrl)
                .thumbnail(compressRequest)
                .listener(listener).into(imageview);

    }

    public static void loadGroupHeader(Fragment fragment, String path, ImageView iv) {
        Glide.with(fragment).load(path).error(ERROR_GROUP_RESOURCES_ID).placeholder(ERROR_GROUP_RESOURCES_ID)
                .fitCenter().transform(new GlideCircleTransform(fragment.getActivity())).into(iv);
    }

    public static void loadGroupHeader(Fragment fragment, File path, ImageView iv) {
        Glide.with(fragment).load(path).error(ERROR_GROUP_RESOURCES_ID).placeholder(ERROR_GROUP_RESOURCES_ID)
                .fitCenter().transform(new GlideCircleTransform(fragment.getActivity())).into(iv);
        Glide.with(fragment).resumeRequests();
    }

    public static void loadGroupHeader(Context context, File path, ImageView iv) {
        Glide.with(context).load(path).error(ERROR_GROUP_RESOURCES_ID).placeholder(ERROR_GROUP_RESOURCES_ID)
                .fitCenter().transform(new GlideCircleTransform(context)).into(iv);
    }

    public static void loadGroupHeader(Context context, String path, ImageView iv) {
        Glide.with(context).load(path).error(ERROR_GROUP_RESOURCES_ID).placeholder(ERROR_GROUP_RESOURCES_ID)
                .fitCenter().transform(new GlideCircleTransform(context)).into(iv);
    }
    public static Bitmap getNetVideoBitmap(String videoUrl) {
        Bitmap bitmap = null;

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            //根据url获取缩略图
            retriever.setDataSource(videoUrl, new HashMap());
            //获得第一帧图片
            bitmap = retriever.getFrameAtTime(5000);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } finally {
            retriever.release();
        }
        return bitmap;
    }


}
